﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace StarWarsGame
{
    class GameLogic
    {
        public Vector3 lightDirection = new Vector3(3, -2, 5);

        public Matrix viewMatrix;
        public Matrix projectionMatrix;

        public void SetUpCamera(GraphicsDevice device)
        {
            viewMatrix = Matrix.CreateLookAt(new Vector3(20, 13, -5), new Vector3(8, 0, -7), new Vector3(0, 1, 0));
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.2f, 500.0f);
        }

        public float GameSpeed = 0.5f;

        public bool isSecondInFOVofFirst(Vector3 posFirst, Vector3 facingFirst, Vector3 posSecond, float fov)
        {
            Vector3 tempVect = posSecond - posFirst;
            tempVect.Normalize();

            return Vector3.Dot(facingFirst, tempVect) >= Math.Cos((double)fov);
        }

        public bool CheckIfInView(Vector3 posFirst, Vector3 facingFirst, Vector3 posSecond, float viewRange, float viewingAngle)
        {          
            // Checks if object is in field of view of the camera
            if (isSecondInFOVofFirst(posFirst, facingFirst, posSecond, MathHelper.ToRadians(viewingAngle)) == true)
            {
                Vector3 FirstToSecond = posSecond - posFirst;
                // Checks if object is within the object's viewing range
                if (FirstToSecond.LengthSquared() < viewRange)
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
        

    }
}
